<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="关键字" prop="keyword">
        <el-input
          size="mini"
          v-model="queryParams.keyword"
          placeholder="请输入问题/工单编号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单状态" prop="status">
        <el-select v-model="queryParams.status" clearable placeholder="请选择状态" size="mini">
          <el-option
            v-for="dict in dict.type.word_order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          format="yyyy-MM-dd"
          value-format="timestamp"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
          size="mini"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="序号" type="index" align="center" min-width="5%"/>
      <el-table-column label="工单编号" align="center" prop="wordOrderSn" key="wordOrderSn"/>
      <el-table-column label="工单状态" align="center" prop="status" key="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.word_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="问题描述" align="center" prop="problem" key="problem">
        <template slot-scope="scope">
          <el-tooltip :content="scope.row.problem" placement="top">
            <div>
              {{ scope.row.problem }}
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="问题图片" align="center" key="problemImg">
        <template slot-scope="scope">
          <image-preview v-if="scope.row.problemImg" class="list-img" :src="scope.row.problemImg"/>
          <span v-else>暂无</span>
        </template>
      </el-table-column>
      <el-table-column label="联系方式" align="center" prop="phone" key="phone"/>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客服人员" align="center" prop="replyUserId" key="replyUserId">
        <template slot-scope="scope">
          <div>客服昵称：{{ scope.row.nickName }}</div>
          <div>客服工号：{{ scope.row.userCode }}</div>
        </template>
      </el-table-column>
      <el-table-column label="回复内容" align="center" prop="replyContent" key="replyContent">
        <template slot-scope="scope">
          <el-tooltip :content="scope.row.replyContent" placement="top">
            <div>
              {{ scope.row.replyContent }}
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="回复时间" align="center" prop="replyTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.replyTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status === '0'"
            size="mini"
            type="text"
            @click="handleEdit(scope.row)"
            v-hasPermi="['system:wordOrder:edit']"
          >回复</el-button>
          <el-button
            v-if="scope.row.status === '3'"
            size="mini"
            type="text"
            @click="doneEdit(scope.row)"
            v-hasPermi="['system:wordOrder:edit']"
          >已处理</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleInfo(scope.row)"
            v-hasPermi="['system:wordOrder:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:wordOrder:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog title="工单详情" :visible.sync="detailOpen" width="50%" append-to-body>
      <el-form ref="form" :model="detail" label-width="100px">
        <el-row :gutter="24">
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="工单编号: ">
                {{detail.wordOrderSn}}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="工单状态: ">
                <dict-tag :options="dict.type.word_order_status" :value="detail.status"/>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="24">
            <div class="grid-content bg-purple">
              <el-form-item label="问题描述: ">
                {{detail.problem}}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="24">
            <div class="grid-content bg-purple">
              <el-form-item label="详情图: ">
                <span v-if="detail.problemImg === null || detail.problemImg === ''">暂无</span>
                <div class="viewer-list-img" v-else>
                  <viewer :images="detail.problemImg">
                    <image-preview v-for="src in detail.problemImg" :src="src" :key="src" class="detail-img"/>
                  </viewer>
                </div>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="联系方式: ">
                {{ detail.phone }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="创建时间: ">
                {{ detail.createTime }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="客服昵称: ">
                {{ detail.nickName }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="客服工号: ">
                {{ detail.userCode }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="回复内容: ">
                {{ detail.replyContent }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="回复时间: ">
                {{ detail.replyTime }}
              </el-form-item>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="detailOpen = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="工单回复" :visible.sync="editOpen" width="30%" append-to-body>
      <el-form ref="form" :model="editForm" label-width="100px">
        <el-form-item label="工单状态:">
          <el-radio-group v-model="editForm.status">
            <el-radio key="1" value="1" label="1">已回复已处理</el-radio>
            <el-radio key="2" value="2" label="2">无需回复已处理</el-radio>
            <el-radio key="3" value="3" label="3">已回复待处理</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="回复内容:" v-if="editForm.status !== '2'">
          <el-input type="textarea" placeholder="请输入回复内容" v-model="editForm.replyContent" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="submitEditForm">确 定</el-button>
        <el-button size="mini" @click="editOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { pageList,getInfo,remove,edit } from '@/api/triones/wordOrder'
export default {
  name: 'index',
  dicts: ['word_order_status'],
  data() {
    return {
      loading: false,
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: undefined,
        status: undefined
      },
      dataList: [],
      total: 0,
      detail: {},
      detailOpen: false,
      editForm: {},
      editOpen: false,
      dateRange: [],
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 工单列表 */
    getList() {
      this.loading = true;
      this.addDateRange(this.queryParams, this.dateRange);
      pageList(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    /** 检索 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 去修改 */
    handleEdit(row) {
      this.reset();
      this.editForm.id = row.id;
      this.editOpen = true;
    },
    /** 提交修改 */
    submitEditForm() {
      edit(this.editForm).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.editOpen = false;
        this.reset();
        this.getList();
      })
    },
    /** 详情 */
    handleInfo(row) {
      this.reset();
      const id = row.id;
      getInfo(id).then(response => {
        this.detail = response.data;
        this.detail.problemImg = response.data.problemImg ? response.data.problemImg.split(',') : [];
        this.detailOpen = true;
      })
    },
    doneEdit(row) {
      this.editForm.id = row.id;
      this.editForm.status = '1';
      this.$modal.confirm('是否确认工单编号为"' + row.wordOrderSn + '"的数据项已完成处理？').then(() => {
        this.loading = true;
        edit(this.editForm).then(response => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("操作成功");
        })
      });
    },
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除工单编号为"' + row.wordOrderSn + '"的数据项？').then(() => {
        this.loading = true;
        return remove(id);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    reset() {
      this.editForm = {
        id: undefined,
        status: '1',
        replyContent: undefined
      }
      this.resetForm("form");
    }
  }
}
</script>
<style scoped lang="scss">
.el-tooltip {
  overflow: hidden;
  display: -webkit-box;
  text-overflow: ellipsis;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  white-space: normal;
}

.info-box {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  .img-box {
    img {
      width: 35px;
      height: 35px;
      border-radius: 50%;
      vertical-align: middle;
    }
  }
  .content-box {
    font-size: 12px;
    padding-left: 10px;
  }
}
</style>
